﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace HPKaraoke
{
    public class PlayListGUI
    {
        protected PlayListController Controller { get; set; }

        private OutlineTextControl _lbDisplay;

        private ucLyricDisplayControl _lyric1;
        private ucLyricDisplayControl _lyric2;


        protected void MediaEndedFunction()
        {
            NextSong();
            PlayCurrentSong();
        }

        public PlayListGUI()
        {
            Controller = new PlayListController();
        }

        public void SetControlDisplayLyrics(ucLyricDisplayControl lyric1, ucLyricDisplayControl lyric2)
        {
            _lyric1 = lyric1;
            _lyric2 = lyric2;
        }

        public void SetControlDisplayListSongs(OutlineTextControl lb)
        {
            _lbDisplay = lb;
        }
            
        public void PlayCurrentSong()
        { 
            SongGUI s = Controller.GetCurrentSong();
            if (s != null)
            {
                s.SetControl(_lyric1, _lyric2);
                s.SetMediaEndedFunction(MediaEndedFunction);
                if (!s.IsPlaying)
                    Controller.PlayCurrentSong();
                _lbDisplay.Text = Controller.SelectStringOfListAvaiSong();
            }
        }


        /// <summary>
        /// Chuyển qua bài tiếp theo
        /// Chỉ chuyển qua bài chứ không tự động play
        /// </summary>
        public void NextSong()
        {
            if (Controller.NumberSongRemain > 0)
            {
                Controller.NextSong();
                ClearOldLyric();
            }
        }

        public void PreviousSong()
        {
            if (!Controller.IsEmpty)
            {
                Controller.PreviousSong();
                ClearOldLyric();
            }
        }

        private void ClearOldLyric()
        {
            _lyric1.BackgroundText.Text = "";
            _lyric1.AnimationText.Text = "";
            _lyric2.BackgroundText.Text = "";
            _lyric2.AnimationText.Text = "";
        }

        public void Stop()
        {
            Controller.Stop();
        }

        public void PauseOrResume()
        {
            Controller.PauseOrResum();
        }

        public int Add(SongGUI s)
        {
            //_lbDisplay.Content =         
            int temp = Controller.Add(s);
            //string listString = Controller.SelectStringOfListAvaiSong();
            //_lbDisplay.Content = listString;
            return temp;
        }

    }
}
